<template>
	<view>
		  <view class="login-container">
		      <view class="login-box">
		        <u-button type="primary" @click="ddlogin" class="login-button">获取钉钉信息</u-button>
				<br/>
				<input v-model="userId" type="text" placeholder="ID" class="input-field" disabled/>
		        <input v-model="mobile" type="text" placeholder="手机号" class="input-field" disabled/>
		        <input v-model="username" type="text" placeholder="用户名" class="input-field" disabled/>
		        <!-- <input v-model="password" type="password" placeholder="密码" class="input-field" /> -->
		        <u-button type="primary" @click="login" class="login-button">登录</u-button>
				<br/>
		        <!-- <u-button type="primary" @click="test(0)" class="login-button">正式登录</u-button> -->
		        <!-- <u-button type="primary" @click="test(2)" class="login-button">测试登录</u-button> -->
		        <!-- <u-button type="primary" @click="a" class="login-button">查看信息</u-button> -->
		      </view>
		    </view>
	</view>
</template>

<script>
	import * as dd from 'dingtalk-jsapi'
	// import { initDingH5RemoteDebug } from "dingtalk-h5-remote-debug";
	export default {
		data() {
			return {
				 mobile: '',
				 username: '',
				 password: '',
				 code: '20581e1c262e317baca7d41ab36a1fc2',
				 userInfo: {},
				 userId: '',
			}
		},
		onLoad() {
			// initDingH5RemoteDebug();
			// dd.config()
			// alert('onLoad')
			var that = this
			dd.ready(function() {
				// alert('dd.ready')
				console.log('dd.ready')
			    dd.runtime.permission.requestAuthCode({
			        corpId: "ding81f5b58b1546a0fd35c2f4657eb6378f", // 企业id
			        onSuccess: function (info) {
						// alert('dd.ready:info='+JSON.stringify(info))
						// alert('dd.ready:info.code='+info.code)
						that.code = info.code // 通过该免登授权码可以获取用户身份
						console.log(that.code)
						that.ddlogin()
						// alert('dd.ready:code='+that.code)
			        },
				});
			});
		},
		methods: {
			ddlogin(){
				// alert('ddlogin:code='+this.code)
				if(this.code=='' || this.code==undefined){
					uni.showToast({
					   title: '初始化失败',
					   icon: 'error',
					   duration: 2000
					});
					return;
				}
				uni.request({
				  url: 'http://125.64.76.148:8125/KXJDDDApi/ddlogin?code='+this.code, // 替换为实际的接口URL
				  method: 'GET',
				  data: {},
				  success: (res) => {
					  console.log(res)
					  // alert('success:'+JSON.stringify(res))
					  this.userInfo = JSON.parse(res.data.userInfo)
					  this.username = this.userInfo.result.name
					  this.mobile = this.userInfo.result.mobile
					  this.userId = res.data.userId
				  },
				  fail(err) {
					  alert('fail:'+err)
				  	console.log(err)
				  }
				})
			},
			test(flg) {
				uni.setStorageSync('userInfo',
				    {
						ID: this.userId || "1",
						PHONE: this.mobile || "13551155820",
						NAME: this.username || "杨俊林",
						TEST: flg==1 ? true : false,
					}
				);
				uni.setStorageSync('test',flg==1 ? true : false);
				uni.switchTab({
				  url: '/pages/index/index'
				});
			},
			 login() {
				 if(this.userId=='' || this.userId==undefined
				 || this.mobile=='' || this.mobile==undefined
				 || this.username=='' || this.username==undefined){
					 uni.showToast({
					    title: '用户信息不全，请补充',
					    icon: 'error',
					    duration: 2000
					 });
					 return;
				 }
				 let GRP1 = {
				 		GRP1: [{
							"ID": this.userId,//"5",
							"PHONE": this.mobile,
							"NAME": this.username,
							// "PHONE": "12345687",
							// "NAME": "TEST1",
				 	}]
				 }
				 let GRP1Json = JSON.stringify(GRP1)
				 uni.request({
				   url: 'http://125.64.76.148:8125/KXJDX3Api/x3api'+"?_t="+new Date().getTime(), // 替换为实际的接口URL
				   method: 'POST',
				   data: {
				 	"username":"APIUSER",
				 	"passwd":"123",
				 	"publicName":"WSGENYDUR",
				 	"method":"run",
				 	"test":uni.getStorageSync('test') ? "true" : "false",
				 	"inputXml":JSON.stringify(GRP1)
				   },
				   success: (res) => {
					if(res.data.status=="error"){
						alert(res.data.message);
						return false;
					}else{
						   // console.log(res.data.message,'res.data.message');
						let loginData = JSON.parse(res.data.message) //将JSON字符串转为对象
						// console.log(loginData,'loginData');
						let a = loginData.GRP1
						// console.log(a,'a');
						if(loginData.GRP2.WOK==0){
							// 显示创建成功的消息提示
							 uni.showToast({
							   title: '登录成功',
							   icon: 'success',
							   duration: 2000
							});
							// this.username=''
							// this.password=''
							// 登录成功后的跳转逻辑
							uni.switchTab({
							  url: '/pages/index/index'
							});
						 // 假设登录成功后返回的用户信息
							const userInfo = {
								"ID": this.userId,//"5",
								"PHONE": this.mobile,
								"NAME": this.username,
							  // ID: "5",
							  // PHONE: "12345687",
							  // NAME: "TEST1",
							};
						 // 保存用户信息到本地存储
							uni.setStorage({
							  key: 'userInfo',
							  data: userInfo,
							  success: () => {
								  uni.showToast({
									 title: '用户信息保存成功',
									 icon: 'success',
									 duration: 2000
								  });
								// console.log('用户信息保存成功');
							  },
							  fail: (err) => {
								// console.error('用户信息保存失败', err);
								uni.showToast({
								   title: '用户信息保存失败',
								   icon: 'error',
								   duration: 2000
								});
							  }
							});
						}else{
							uni.showToast({
							   title: '登录失败',
							   icon: 'error',
							   duration: 2000
							});
						}
					}
				   },
				   fail: (err) => {
				 	// 请求失败的处理逻辑
				 	console.error(err);
				   }
				 });
			},
			// a(){
			// 	uni.getStorage({
			// 	  key: 'userInfo',
			// 	  success: function (res) {
			// 	    console.log('获取用户信息成功', res.data);
			// 	    // 在这里可以对获取到的用户信息进行处理
			// 	  },
			// 	  fail: function (err) {
			// 	    console.error('获取用户信息失败', err);
			// 	  }
			// 	});
			// }
		}
	}
</script>

<style>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
  }

  .login-box {
	margin-top: -270rpx;
    width: 600rpx;
    padding: 20rpx;
    background-color: #f2f2f2;
    border-radius: 10rpx;
  }

  .input-field {
    width: 98%;
    margin-bottom: 50rpx;
    padding: 10rpx;
    border: 1rpx solid #ccc;
    border-radius: 5rpx;
  }

  .login-button {
    width: 100%;
    padding: 10rpx;
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 5rpx;
    text-align: center;
  }
</style>
